import api from '../../utils/api'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userName: "",
        time: "",
        detail: "",
        itemImg: "",
        price: "",
        goodId: "",
        userId: "", // 存储 买家/卖家 的id（非工号）
        isIdle: "", // 0-在售，1-求购
        isDisabled: false,
        isLoading:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu()
        var index = options.index
        var latestPostIndex = options.latestPostIndex // 好物上新
        var isIdle1 = options.isIdle // 0-在售 1-求购
        var that = this
        that.setData({
            isIdle: isIdle1,
            isDisabled: false,
            isLoading:false
        })
        if (index) { //从商品列表进入
            var httpApi = ""
            var httpData = {}
            var goods = wx.getStorageSync('goods')
            if (isIdle1 == 0) {
                httpApi = api.getGoodsDetail
                httpData = {
                    goodsId: goods[index].goodsId
                }
            } else if (isIdle1 == 1) { // 求购
                httpApi = api.getPurchaseGoodsDetail
                httpData = {
                    purchaseInfoId: goods[index].goodsId
                }
            }

            wx.request({
                url: httpApi,
                data: httpData,
                method: "POST",
                header: {
                    'content-type': 'application/json;charset=UTF-8',
                    'token': wx.getStorageSync('token')
                },
                success(res) {

                    that.setData({
                        time: res.data.data[0].releaseTime,
                        detail: res.data.data[0].description,
                        itemImg: res.data.data[0].imgUrl,
                        price: res.data.data[0].price,
                        peakPrice: res.data.data[0].peakPrice,
                        bottomPrice: res.data.data[0].bottomPrice,
                        title: res.data.data[0].title,
                        status: res.data.data[0].status,
                        goodId: goods[index].goodsId,
                        userName: goods[index].userName,
                        userId: goods[index].userId
                    })
                }
            })
        }

        if (latestPostIndex) { // 从好物上新进入
            var goods = wx.getStorageSync('newGoods')
            
            wx.request({
                url: api.getGoodsDetail,
                data: {
                    goodsId: goods[latestPostIndex].goodsId
                },
                method: "POST",
                header: {
                    'content-type': 'application/json;charset=UTF-8',
                    'token': wx.getStorageSync('token')
                },
                success(res) {
                    that.setData({
                        time: res.data.data[0].releaseTime,
                        detail: res.data.data[0].description,
                        itemImg: res.data.data[0].imgUrl,
                        price: res.data.data[0].price,
                        peakPrice: res.data.data[0].peakPrice?res.data.data[0].peakPrice:"",
                        bottomPrice: res.data.data[0].bottomPrice?res.data.data[0].bottomPrice:"",
                        title: res.data.data[0].title,
                        status: res.data.data[0].status,
                        goodId: goods[latestPostIndex].goodsId,
                        userName: res.data.data[0].name + goods[latestPostIndex].workno,
                        userId: goods[latestPostIndex].userId
                    })
                }
            })


            // that.setData({
            //     userName: goods[latestPostIndex].userName,
            //     time: goods[latestPostIndex].goodsTime,
            //     detail: goods[latestPostIndex].description,
            //     itemImg: goods[latestPostIndex].imgUrl,
            //     price: goods[latestPostIndex].price,
            //     goodId: goods[latestPostIndex].goodsId
            // })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    submit: function (e) {
        var that = this
        var loginUser = wx.getStorageSync('logindata')
        that.setData({
            isDisabled:true,
            isLoading:true
        })
        if(that.data.isIdle == 0) {
            wx.request({
                url: api.goodsBook,
                method: "POST",
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token')
                },
                data: {
                    goodsId: that.data.goodId
                },
                success(res) {
                    if (res.data.state == 1) {
                        wx.showToast({
                            title: '下单成功！',
                        })
                        setTimeout(function () {
                            wx.navigateBack({
                                delta: 1,
                            })
                        }, 1500) //延迟时间 这里是1秒
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'error',
                        })
                    }
    
                }
            })
        } else if(that.data.isIdle == 1) {
            wx.request({
                url: api.markDemand,
                method: "POST",
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token')
                },
                data: {
                    id: that.data.goodId,
                    seller_id: loginUser.data.id,
                    buyer_id: that.data.userId
                },
                success(res) {
                    if (res.data.state == 1) {
                        wx.showToast({
                            title: '出售成功！',
                        })
                        setTimeout(function () {
                            wx.navigateBack({
                                delta: 1,
                            })
                        }, 1500) //延迟时间 这里是1秒
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'error',
                        })
                    }
    
                }
            })
        }
        setTimeout(function() {
            that.setData({
                isDisabled:false,
                isLoading: false
            })
        },2000)
    },
    previewPic: function (e) {
        var imgUrls = []
        imgUrls = [this.data.itemImg]
        wx.previewImage({
            urls: imgUrls, //需要预览的图片http链接列表，注意是数组
            current: '', // 当前显示图片的http链接，默认是第一个
            success: function (res) {},
            fail: function (res) {},
            complete: function (res) {},
        })

    },
})